package com.woniuxy.commons.swagger;

import com.woniuxy.commons.jwt.UserConst;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Resource
    SwaggerYml swaggerYml;
    @Bean
    public Docket createDocket(){
        //创建文档对象
        Docket docket=new Docket(DocumentationType.SWAGGER_2);
        List<Parameter> ps = Arrays.asList(
                new ParameterBuilder().parameterType("header").name(UserConst.USER_TOKEN)
                        .modelRef(new ModelRef("string")).description("token").required(false).build()
        );
        //创建文档对象里面的文档基本西悉尼
        ApiInfo apiInfo=new ApiInfoBuilder()
                .contact(new Contact(swaggerYml.getName(),swaggerYml.getUrl(),swaggerYml.getEmail()))
                .description(swaggerYml.getDescription())
                .title(swaggerYml.getTitle())
                .build();
        //把文档对象设置进去生成文档的接口路径扫描
        docket.apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerYml.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(ps);
        return docket;
    }
}
